/**
 * Created by szc on 2016/12/14.
 */
declare function
socketSend(json: any, fn: any);
declare function
setReconFn(fn: any);
declare function
serverDispatch(action: any, postData: any, fn: any);
declare function
sleep(s: any);
declare function
addKeyDownEvent(_key: any, fn: any);
declare function
sliderOpen();

class MainPanel extends egret.DisplayObjectContainer {
    private setting_b;
    public gunPanle: GunPanel;
    public leftPanle;
    public freePanle;
    public crystalPanle: CrystalPanle;
    public tdata = null;
    public context: Main;
    public soundctr: SoundDeal;
    public gunDeal = new GunDeal(this);
    public data: any;

    public moreBtn:More;

    public constructor(context: Main, data: any) {
        super();
        this.data = data;
        this.context = context;
        this.soundctr = new SoundDeal(this);
        this.soundctr.playBgm1();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        var point = this;
    }

    private onAddToStage(event: egret.Event) {
        var data = this.data;
        /**
         * 设置短线重连的界面
         */
        setReconFn(function () {
            var test = new Reconnect(point);
            point.addChild(test);
        });
        /**
         * 加载主界面的背景
         */
        this.addChild(new MainBg());
        /**
         * 加载滚筒页面
         */
        this.creatGunPanel.call(this, data);


        var point = this;

        /**
         * 加载外框和整体的外置功能有关的件
         */
        this.createDecorate();


        this.crystalPanle = new CrystalPanle(this);
        this.addChild(this.crystalPanle);

        // //设置免费游戏的画面
        this.freePanle = new FreePanle();
        this.freePanle.x = 0;
        this.freePanle.y = 0;
        this.addChild(this.freePanle);

        //设置剩余钱数
        this.leftPanle = new LeftPanle();
        this.leftPanle.x = 90;
        this.leftPanle.y = 528;
        // this.leftPanle.alpha = 0;
        this.addChild(this.leftPanle);
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapAction, this);

        // point.creatGunPanel.call(point, data);
        point.leftPanle.setMoney(data.Value.TokenInfo.Balance);

        if (data.Actions) {

            if (data.Actions.freeslot) {
                point.freePanle.setFreeNum(data.Actions.freeslot.total, data.Actions.freeslot.count);
            } else {
                point.freePanle.setFreeNum(0, 0);
            }
        } else {
            point.freePanle.setFreeNum(0, 0);
        }
        /**
         * 面板可以计算当前的总注
         */
        point.crystalPanle.ca_zhu();

        if (point.freePanle.ishasFree()) {
            var test = new Reload(point);
            point.addChild(test);
        }

        this.addChild(new MainTop());
        this.setSetting();

        var moreBtn = this.moreBtn = new More(this);
        moreBtn.x = 1000;
        moreBtn.y = 80;
        this.addChild(moreBtn)

        let walkingLight:WalkingLight = new WalkingLight()
        this.addChild(walkingLight);

        if(this.data.Marquees && this.data.Marquees != ""){
            walkingLight.showWalking(this.data.Marquees);
        }
        

    }

    /**
     * 触摸控制器
     * @param evt
     */
    public tapAction(evt: egret.TouchEvent): void {
        var actionName = evt.target.name;
        if (this.crystalPanle.isAutoRotation()) {
            // this.crystalPanle.closeAutoRotation();
            this.soundctr.playSound("click_mp3");
            return;
        }
        console.log(actionName);
        switch (actionName) {
            case "setting": this.openNewchildPanle(actionName); break;
            case "234_line": this.openNewchildPanle(actionName); break;
            case "paylist": this.openNewchildPanle(actionName); break;
            case "direction": this.openNewchildPanle(actionName); break;
            case "unit_stop": this.openNewchildPanle(actionName); break;
            case "rotation": this.gunDeal.gunRotate(); break;
            case "stoproation": this.gunDeal.gunStop(); break;
            case "auto_rotation_btn": this.crystalPanle.setAutoRotation(); break;
            case "soundctr": this.soundctr.ishasSound(); break;
        }
        if (actionName) {
            this.soundctr.playSound("click_mp3");
        }
    }

    /**
     * 打开一个配置页面
     * @param str
     */
    public openNewchildPanle(str: any, text: any = null): void {
        var temp = null;
        this.crystalPanle.closeAutoRotation();
        switch (str) {
            case "setting": temp = new AutoGameSys(this); break;
            case "234_line": temp = new ExplainPromptPanle(this); break;
            case "paylist": temp = new PaylistPanel(this); break;
            case "direction": temp = new DirectionPanle(this); break;
            case "unit_stop": temp = new AutoGameSys(this); break;
            case "lianxian": temp = new Lianxian(this, text); break;
        }
        this.context.openChildPanle(temp);
    }


    /**
     * 创建滚动的面板
     * @param data
     */
    private creatGunPanel(data: any): void {

        var gunPanelWidth = 0.80;
        var gunPanel = new GunPanel(this.stage.stageWidth * gunPanelWidth +20 , this.stage.stageHeight, data, this);
        gunPanel.x = 80;
        gunPanel.y = 80;
        this.gunPanle = gunPanel;
        this.addChild(gunPanel);

        // this.gunPanle.openAm(1);
    }



    private createDecorate(): void {

        // var decontain = new egret.DisplayObjectContainer();
        // this.addChild(decontain);
        // // 加载主面板
        // var imgCon = [];
        // imgCon['url'] = 'buildings_png';
        // imgCon['width'] = this.stage.stageWidth * 0.90;
        // imgCon["x"] = 20;
        // imgCon['height'] = this.stage.stageHeight;
        // decontain.addChild(new ImageBitmap(imgCon).getImgPoint());

        // var point = this;
    }

    public setSetting() {
        var decontain = new egret.DisplayObjectContainer();
        this.addChild(decontain);
        var column_width = 72;
        var column_height = 162;
        var column_y = 100;
        // //加载左侧柱子和装饰
        // var img2 = new eui.Image();
        // img2.width = column_width;
        // img2.height = column_height;
        // img2.source = "243_line_png";
        // img2.touchEnabled = true;
        // img2.name = "234_line";
        // img2.x = 15;
        // img2.y = column_y;
        // decontain.addChild(img2);

        // 设置打开游戏说明的图标
        // var _btn1 = new eui.Image();
        // this.soundctr.soundBtn = _btn1;
        // _btn1.source = "wenhao_png";
        // _btn1.width = 30;
        // _btn1.height = 40;
        // _btn1.x = 40;
        // _btn1.y = 350;
        // _btn1.touchEnabled = true;
        // _btn1.name = "direction";
        // decontain.addChild(_btn1);


        // // //加载右侧侧柱子和装饰

        // var img2 = new eui.Image();
        // img2.width = column_width;
        // img2.height = column_height;
        // img2.source = "243_line_png";
        // img2.touchEnabled = true;
        // img2.name = "234_line";
        // img2.x = this.stage.stageWidth - 15 - column_width;
        // img2.y = column_y;
        // decontain.addChild(img2);

        //设置声音
        var _btn1 = new eui.Image();
        this.soundctr.soundBtn = _btn1;

        if (this.soundctr.isOpen()) {
            _btn1.source = "m_sound_png";
        } else {
            _btn1.source = "m_sound_close_png";
        }
        _btn1.width = 50;
        _btn1.height = 50;
        _btn1.x = 15;
        _btn1.y = 350;
        _btn1.touchEnabled = true;
        _btn1.name = "soundctr";
        decontain.addChild(_btn1);

        var sliderOpen = new eui.Image();
        sliderOpen.source = "jiantou_png";
        sliderOpen.scaleX = 0.56;
        sliderOpen.scaleY = 0.56;
        sliderOpen.y = this.height/2 - sliderOpen.height/2 - 50;
        sliderOpen.touchEnabled = true;
        sliderOpen.addEventListener(egret.TouchEvent.TOUCH_TAP,this.sliderOpenEvent,this);
        this.addChild(sliderOpen);


        // //设置按钮
    //     var _btn = new eui.Button();
    //     this.setting_b = _btn;
    //     _btn.skinName = "m_setting";
    //     _btn.width = 50;
    //     _btn.height = 50;
    //     _btn.x = this.stage.stageWidth - 10 - column_width;;
    //     _btn.y = 400;
    //     _btn.touchEnabled = true;
    //     _btn.name = "setting";
    //     decontain.addChild(_btn);
    }

    private sliderOpenEvent(evt:egret.Event)
    {
        sliderOpen();
    }

}